<?php

/**
 * MultipleAnswer form base class.
 *
 * @package    casesonline
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 16976 2009-04-04 12:47:44Z fabien $
 */
class BaseMultipleAnswerForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                   => new sfWidgetFormInputHidden(),
      'multiple_question_id' => new sfWidgetFormInputHidden(),
      'feedback_id'          => new sfWidgetFormInputHidden(),
      'answer'               => new sfWidgetFormTextarea(),
      'correct'              => new sfWidgetFormInputCheckbox(),
      'order_nr'             => new sfWidgetFormInput(),
      'source_list'          => new sfWidgetFormPropelChoiceMany(array('model' => 'Step')),
    ));

    $this->setValidators(array(
      'id'                   => new sfValidatorPropelChoice(array('model' => 'MultipleAnswer', 'column' => 'id', 'required' => false)),
      'multiple_question_id' => new sfValidatorPropelChoice(array('model' => 'MultipleQuestion', 'column' => 'id', 'required' => false)),
      'feedback_id'          => new sfValidatorPropelChoice(array('model' => 'Feedback', 'column' => 'id', 'required' => false)),
      'answer'               => new sfValidatorString(),
      'correct'              => new sfValidatorBoolean(),
      'order_nr'             => new sfValidatorInteger(),
      'source_list'          => new sfValidatorPropelChoiceMany(array('model' => 'Step', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorPropelUnique(array('model' => 'MultipleAnswer', 'column' => array('order_nr')))
    );

    $this->widgetSchema->setNameFormat('multiple_answer[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'MultipleAnswer';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['source_list']))
    {
      $values = array();
      foreach ($this->object->getSources() as $obj)
      {
        $values[] = $obj->getStepId();
      }

      $this->setDefault('source_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveSourceList($con);
  }

  public function saveSourceList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['source_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(SourcePeer::MULTIPLE_ANSWER_ID, $this->object->getPrimaryKey());
    SourcePeer::doDelete($c, $con);

    $values = $this->getValue('source_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Source();
        $obj->setMultipleAnswerId($this->object->getPrimaryKey());
        $obj->setStepId($value);
        $obj->save();
      }
    }
  }

}
